<?php
/**
* 用户注册
* @author moufer<moufer@163.com>
* @copyright Moufer Studio(www.modoer.com)
*/
!defined('IN_MUDDER') && exit('Access Denied');
define('SCRIPTNAV', 'reg');

// if($user->isLogin) redirect('member_reg_logined');

$forward = $_GET['forward'] ? $_GET['forward'] : $_G['cfg']['siteurl'];
if(strposex($forward,'op=logout') || !strposex($forward, $_G['web']['domain'])) {
    $forward = $_G['cfg']['siteurl'];
}
$forward = _T(rawurldecode(rawurldecode($forward)));

$mobile = $_G['loader']->model('howcan:user');

$_G['loader']->helper('validate');

switch($_GET['op']) {
case 'check_username':
    if(!$username = trim($_POST['username'])) {
        echo lang('member_reg_ajax_name_empty'); exit;
    }
    if($_G['charset'] != 'utf-8') {
        $_G['loader']->lib('chinese', NULL, FALSE);
        $CHS = new ms_chinese('utf-8', $_G['charset']);
        $username = _T($CHS->Convert($username));
    }
    $user->check_username($username, true);
    if($user->check_username_exists($username)) {
        echo lang('member_reg_ajax_name_exists');
        exit;
    }
    echo lang('member_reg_ajax_name_normal'); exit;
    break;
case 'check_email':
    if(!$email = trim($_POST['email'])) {
        echo lang('member_reg_ajax_email_empty'); exit;
    }
    if(!validate::is_email($email)) {
        echo lang('member_reg_ajax_email_invalid'); exit;
    }
    if(!$MOD['existsemailreg'] && $user->check_email_exists($email)) {
        echo lang('member_reg_ajax_email_exists');
        exit;
    }
    echo lang('member_reg_ajax_email_normal'); exit;
    break;
case 'reg':
    break;
default:
	
	// 自动注册手机用户
	if (isset($_GET['devid'])) {
		$devid = $_GET['devid'];
		if (!$mobile->check_devid_exists($devid)) {
			echo $uid = $mobile->m_auto_reg($mobile->get_post($_GET));
			if ($uid>0) {
				$token = md5($devid.'howcan');
				$mobile->m_login($uid,$token);
				echo json_encode(array('token'=>$token));
			}
		}
	}
    break;
}

?>